/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.univnantes.alma.arbres;

import java.util.Iterator;
import java.util.Vector;
import java.util.NoSuchElementException;

/**
 *
 * @author judu
 */
class ArbreTMIteratorBFS<T> implements Iterator<Noeud<T>> {

    private Vector<NoeudTM<T>> arbre;
    private NoeudTM<T> current;

    public ArbreTMIteratorBFS(Vector<NoeudTM<T>> arbre) {
        this.arbre = arbre;

        if(!this.arbre.isEmpty()){
            this.current = this.arbre.get(0);
        } else {
            this.current = null;
        }
    }

    private void setUpNext(NoeudTM<T> current){
        int curIndex = current.getIndex()+1;

        while(curIndex < this.arbre.size() && this.arbre.get(curIndex) == null){
            ++curIndex;
        }

        if (curIndex  >= this.arbre.size()){
            this.current = null;
        } else {
            this.current = this.arbre.get(curIndex);
        }
    }

    public boolean hasNext() {
        return (this.current != null);
    }

    public Noeud<T> next() throws NoSuchElementException {
        if(!this.hasNext()) throw new NoSuchElementException("Terminus de l'arbre, vérifiez en sortant que vous n'oubliez aucun bagage.");
        NoeudTM<T> tmp = this.current;
        this.setUpNext(tmp);
        return tmp;
    }

    public void remove() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
